#include "ZSpiritImgFile.h"

CSpiritImgFile::CSpiritImgFile():CImgFile()
{
	this->m_AllDir=0;
	this->m_AllFrame=0;
	this->m_CurDir=0;
	this->m_CurFrame=0;
	this->W=0;
	this->H=0;
	this->m_FrameBmp=NULL;
}

CSpiritImgFile::~CSpiritImgFile()
{
	int i;
	if(m_FrameBmp)
	{
		for(i=0;i<m_AllFrame*m_AllDir;i++)
		{
			delete m_FrameBmp[i];
		}
		delete []m_FrameBmp;
	}
}

CBitBmp *CSpiritImgFile::GetCurBmp()
{
	if(this->m_FrameBmp)
	{
		return m_FrameBmp[m_CurDir*m_AllFrame+m_CurFrame];
	}
	else
	{
		return this;
	}
}

CBitBmp *CSpiritImgFile::GetBmpForFrame(BYTE dir,BYTE frame)
{
	if(this->m_FrameBmp)
	{
		return m_FrameBmp[dir*m_AllFrame+frame];
	}
	else
	{
		return this;
	}
}

bool CSpiritImgFile::SetDirFrame(BYTE dir,BYTE frame)
{
	int i,j;
	if(headInfo==NULL)
		return false;
	W=headInfo->biWidth/frame;
	H=headInfo->biHeight/dir;
	m_FrameBmp=new CBitBmp*[dir*frame];
	m_AllFrame=frame;
	m_AllDir=dir;
	for(i=0;i<dir*frame;i++)
	{
		m_FrameBmp[i]=new CBitBmp();
		m_FrameBmp[i]->SetSize(W,H);
		for(j=0;j<H;j++)
			MoveMemory((char *)m_FrameBmp[i]->BmpData[j][0],(char *)BmpData[(i/frame)*H+j][i%frame*W],sizeof(TColor)*W);
		m_FrameBmp[i]->SetMake(true);
	}
	this->FreeOldDib();
	return true;
}
void CSpiritImgFile::SetCurFrame(BYTE frame)
{
	if(frame>=m_AllFrame)
	{
		if(m_AllFrame==0)
			m_CurFrame=0;
		else
			m_CurFrame=m_AllFrame-1;
	}
	else
		m_CurFrame=frame;
}
void CSpiritImgFile::SetCurDir(BYTE dir)
{
	if(dir>=m_AllDir)
	{
		if(m_AllDir==0)
			m_CurDir=0;
		else
			m_CurDir=m_AllDir-1;
	}
	else
		m_CurDir=dir;
}